/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.adaptor.method.createpropertydefmethod;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.compileunit.CompileUnitGeneratorContext;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.attr.AssoUdtAtrribute;
import com.inspur.edp.cef.api.attr.PropertyGetAtrribute;
import com.inspur.edp.cef.api.repository.INestedRepository;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.collection.GspFieldCollection;
import com.inspur.edp.cef.designtime.api.element.GspAssociation;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.gencommon.ReflectionUtils;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.cef.generator.repository.adaptor.property.DataTypeMappingGenerator;
import com.inspur.edp.cef.generator.repository.dac.DataTypeDacGenContext;
import com.inspur.edp.cef.repository.assembler.AssociationInfo;
import com.inspur.edp.cef.repository.readerwriter.CefMappingReader;
import com.inspur.edp.cef.repository.repo.BaseRootRepository;
import com.inspur.edp.cef.spi.entity.INestedConfig;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import java.util.ArrayList;
import lombok.var;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

//    
//     private IBillState getBillStatusUdtPropertyValue(ICefReader reader)
//     {
//       CefMappingReader udtReader= new CefMappingReader(BillStatusMapping,reader);
//       var u1Repos = new Factory().getRepos("configId");
//       return u1Repos.ReadData(udtReader) as IBillState;
//
//     }
//     
public class CreateUdtDefMethodGenerator extends BasePropertyCreateDefMethodGenerator {
//    private CefPropertyInfo propertyInfo;
    private IGspCommonField gspCommonField;
    private IGspCommonDataType gspCommonDataType;
    private UnifiedDataTypeDef udt;
    private DataTypeDacGenContext dacContext;
    private String nodeCode;
    private java.util.ArrayList<AssociationInfo> associationInfos;
    private static final String varMappingReader = "mappingReader";
    private static final String varRepoName = "u1Repos";
    private static final String varAssUdtName = "udtData";
    private static final String varAss = "assValue";

    public CreateUdtDefMethodGenerator(IGspCommonField gspCommonField, IGspCommonDataType gspCommonDataType, DataTypeDacGenContext dacContext, String nodeCode) {
        this.dacContext = dacContext;
        this.nodeCode = nodeCode;
        this.associationInfos = associationInfos;
        this.gspCommonField = gspCommonField;
        this.gspCommonDataType = gspCommonDataType;
        this.udt = (UnifiedDataTypeDef)gspCommonDataType;
    }

    private String getUdtType(String proertyName) {
        java.util.HashMap<String, java.util.HashMap<String, MediateType>> udtTypes =  getEntityContext().getEnrichedUdtAssoTypes();
        if (!udtTypes.containsKey(nodeCode)) {
            return "";
        }
        java.util.HashMap<String, MediateType> nodeUdtTypes = udtTypes.get(nodeCode);
        if (!nodeUdtTypes.containsKey(proertyName)) {
            return "";
        }
        return nodeUdtTypes.get(proertyName).getClassName();
    }

    private EntityGeneratorContext getEntityContext(){
        return dacContext.getReposContext().getCefContext().getEntityGeneratorContext();
    }

    public static CefPropertyInfo getAssPropertyInfo(java.lang.Class propertyType) {
      var methods = propertyType.getDeclaredMethods();
      int length=methods.length;
      for (int i=length-1; i>=0;i--) {
        var method =methods[i];
        AssoUdtAtrribute assoUdtAtrribute = (AssoUdtAtrribute) ReflectionUtils
            .getMethodAnnotation(method, AssoUdtAtrribute.class);
        PropertyGetAtrribute propertyGetAtrribute = (PropertyGetAtrribute) ReflectionUtils
            .getMethodAnnotation(method, PropertyGetAtrribute.class);
        if (propertyGetAtrribute == null) {
          continue;
        }
        if (assoUdtAtrribute == null) {
          return new CefPropertyInfo(propertyGetAtrribute.propName(), method);
        }
      }
        throw new RuntimeException("GetAssPropertyInfo");
    }
//        for (Method item : propertyType.getMethods()) {
//            AssoUdtAtrribute assoUdtAtrribute = (AssoUdtAtrribute) ReflectionUtils.getMethodAnnotation(item, AssoUdtAtrribute.class);
//            PropertyGetAtrribute propertyGetAtrribute = (PropertyGetAtrribute) ReflectionUtils.getMethodAnnotation(item, PropertyGetAtrribute.class);
//            if (propertyGetAtrribute == null) {
//              continue;
//            }
//            if (assoUdtAtrribute == null) {
//                return new CefPropertyInfo(propertyGetAtrribute.propName(), item);
//            }
//        }
//        throw new RuntimeException("GetAssPropertyInfo");
//    }

    @Override
    protected String getMethodName() {
        return getUdtPropertyMethodName(gspCommonField.getLabelID());
    }

    @Override
    protected TypeInfo getReturnType() {
        return dacContext.getReposContext().getCefContext().getEntityGeneratorContext().getFieldTypeInfo(gspCommonDataType.getCode(), gspCommonField);
    }

    public static String getUdtPropertyMethodName(String propertyName) {
        return "get" + propertyName + "UdtPropertyValue";
    }

    public static String getSetterUdtPropertyMethodName(String propertyName) {
        return "set" + propertyName + "UdtPropertyValue";
    }


    @Override
    protected Block buildMethodBody() {
      addImport(BaseRootRepository.class.getTypeName());
      Block block = ast.newBlock();
        addNewMappingReaderStatement(block);
        addNewRepositoryStatement(block);
        addReturnStatement(block);
        return block;
//		java.util.ArrayList<StatementSyntax> statements = new java.util.ArrayList<StatementSyntax>();
//		AddMappingReaderStatement(statements);
//		AddRepoStatement(statements);
//		AddReturnStatement(statements);
//		return SyntaxFactory.Block(statements.toArray(new StatementSyntax[]{}));
    }

    //	private void test()
//	{
//		CefMappingReader val = new CefMappingReader(IssueWhMapping, reader);
//		INestedRepository val2 = new UdtRepositoryFactory().createRepository("Inspur.GS.Scm.ScmFnd.Warehouse.udt.WarehouseUdt.WarehouseUdt");
//	}
    //	CefMappingReader val = new CefMappingReader(IssueWhMapping, reader);
    private void addNewMappingReaderStatement(Block block) {
        VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
        variableDeclarationFragment.setName(ast.newSimpleName(varMappingReader));
        addNewMappingReaderInitializer(variableDeclarationFragment);
        VariableDeclarationStatement variableDeclarationStatement = ast.newVariableDeclarationStatement(variableDeclarationFragment);
        variableDeclarationStatement.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(CefMappingReader.class).getType(ast));
        block.statements().add(variableDeclarationStatement);
    }

    private void addNewMappingReaderInitializer(VariableDeclarationFragment variableDeclarationFragment) {
        ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
        classInstanceCreation.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(CefMappingReader.class).getType(ast));
        classInstanceCreation.arguments().add(ast.newSimpleName(getMappingFieldName()));
        classInstanceCreation.arguments().add(ast.newSimpleName("reader"));
        variableDeclarationFragment.setInitializer(classInstanceCreation);
    }

    private String getMappingFieldName() {
        return DataTypeMappingGenerator.getFieldName(gspCommonField.getLabelID());
    }

    private void addNewRepositoryStatement(Block block) {
        VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
        variableDeclarationFragment.setName(ast.newSimpleName(varRepoName));
        addNewRepoInitializerStatement(variableDeclarationFragment);

        VariableDeclarationStatement variableDeclarationStatement = ast.newVariableDeclarationStatement(variableDeclarationFragment);
        variableDeclarationStatement.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(INestedRepository.class).getType(ast));
        addImport(INestedRepository.class.getTypeName());
        block.statements().add(variableDeclarationStatement);
    }

    private void addNewRepoInitializerStatement(VariableDeclarationFragment variableDeclarationFragment) {
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        methodInvocation.setName(ast.newSimpleName("getNestedRepository"));
        UnifiedDataTypeDef udtDef = (UnifiedDataTypeDef)gspCommonDataType;
        methodInvocation.arguments().add(GeneratorUtil.getStringLiteral(ast, udtDef.getUdtType()));
        variableDeclarationFragment.setInitializer(methodInvocation);
    }

    private void addNewRepoInitializerCreateRepositoryExpression(MethodInvocation methodInvocation, INestedConfig nestedConfig) {
        MethodInvocation methodInvocation1=ast.newMethodInvocation();
        methodInvocation1.setName(ast.newSimpleName("getUdtRepositoryFactory"));
        methodInvocation1.setExpression(ast.newName("com.inspur.edp.udt.api.UdtManagerUtils"));
        methodInvocation.setExpression(methodInvocation1);
    }

    private void addReturnStatement(Block block) {
        if(gspCommonField.getIsUdt() && gspCommonField.getObjectType() == GspElementObjectType.Association){
            if(gspCommonField.containRefElementNotFromAssoUdt()){
                addAssoUdtReturnStatement(block);
                return;
            }
        }
        addNotAssUdtReturnStatement(block);
    }

    private void addNotAssUdtReturnStatement(Block block) {
        CastExpression castExpression=ast.newCastExpression();
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        methodInvocation.setName(ast.newSimpleName("readData"));
        addImport(CefMappingReader.class.getTypeName());
        addImport(INestedRepository.class.getTypeName());
        methodInvocation.setExpression(ast.newSimpleName(varRepoName));
        methodInvocation.arguments().add(ast.newSimpleName(varMappingReader));
        castExpression.setExpression(methodInvocation);
        TypeInfo info = new TypeInfo(this.udt.getGeneratedEntityClassInfo().getClassFullName());// Utils.getUdtType(udt.getUdtType(), gspCommonField.getUdtID(), dacContext.getReposContext().getCefContext().getCefGenContext());
        info.setUseFullName(true);
        castExpression.setType(info.getType(ast));
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(castExpression);
        block.statements().add(returnStatement);
    }

    private void addAssoUdtReturnStatement(Block block) {
      VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
      variableDeclarationFragment.setName(ast.newSimpleName(varAssUdtName));

      MethodInvocation readDataInvocation = ast.newMethodInvocation();
      readDataInvocation.setExpression(ast.newSimpleName(varRepoName));
      readDataInvocation.setName(ast.newSimpleName("readData"));
      readDataInvocation.arguments().add(ast.newSimpleName(varMappingReader));
      CastExpression castExpression =ast.newCastExpression();
      TypeInfo info = new TypeInfo(this.udt.getGeneratedEntityClassInfo().getClassFullName());//Utils.getUdtType(gspCommonField.getUdtPkgName(), gspCommonField.getUdtID(), dacContext.getReposContext().getCefContext().getCefGenContext());
      info.setUseFullName(true);
      castExpression.setType(info.getType(ast, this.dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext()));
      castExpression.setExpression(readDataInvocation);
      variableDeclarationFragment.setInitializer(castExpression);
      VariableDeclarationStatement variableDeclarationStatement = ast.newVariableDeclarationStatement(variableDeclarationFragment);
      variableDeclarationStatement.setType(info.getType(ast, this.dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext()));

      block.statements().add(variableDeclarationStatement);

        //TODO
//      CefPropertyInfo assoProperty = getAssPropertyInfo(propertyInfo.getPropertyMethod().getReturnType());
//        Class assoType = assoProperty.getPropertyMethod().getReturnType();
        addNewAssDataStatement(block);
//        AssociationInfo assInfo = associationInfos.stream().filter(
//            item -> gspCommonField.getLabelID().equals(item.getSourceColumn())).findFirst().orElse(null);
        GspAssociation assInfo = gspCommonField.getChildAssociations().get(0);
      ArrayList<Statement> statements = new ArrayList<>();
        EntityCreateAssocationDefMethodGenerator.addInitMappingStatement(ast, statements, gspCommonField.getLabelID(), EntityCreateAssocationDefMethodGenerator.varMapping);
        EntityCreateAssocationDefMethodGenerator.addNewMappingReaderStatement(
            ast,
            statements,
            EntityCreateAssocationDefMethodGenerator.getMappingReader(gspCommonField.getLabelID() ),
            EntityCreateAssocationDefMethodGenerator.varMapping);
        EntityCreateAssocationDefMethodGenerator.addNewAdapterStatement(ast, statements, gspCommonField.getLabelID(), "adapter",((SimpleDataTypeDef) gspCommonDataType).getChildAssociations().get(0).getRefObjectCode());

        GspFieldCollection commonFields= gspCommonField.getChildAssociations().get(0).getRefElementCollection();
        for(IGspCommonField field:commonFields)
        {
            if(field.getIsFromAssoUdt())
                continue;
            buildPropAssStatement(ast, statements, assInfo,field,
                    gspCommonField.getLabelID(), gspCommonField.getChildAssociations().get(0), this.dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext());
        }
//        for (Method method : assoType.getMethods()) {
//          PropertyGetAtrribute getAttr = method.getAnnotation(PropertyGetAtrribute.class);
//          if(getAttr == null || !method.getDeclaringClass().equals(assoType)) {
//            continue;
//          }
//          buildPropAssStatement(ast, statements, assInfo, new CefPropertyInfo(getAttr.propName(), method),
//              assoProperty.getPropertyName(), this.dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext());
//        }
      block.statements().addAll(statements);
        ReturnStatement returnStatement = ast.newReturnStatement();
      returnStatement.setExpression(ast.newSimpleName(varAss));
      block.statements().add(returnStatement);
    }

  private void addNewAssDataStatement(Block block) {
      VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
      variableDeclarationFragment.setName(ast.newSimpleName(varAss));
    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(ast.newSimpleType(ast.newName(getUdtType(gspCommonField.getLabelID()))));
    classInstanceCreation.arguments().add(ast.newSimpleName(varAssUdtName));
    variableDeclarationFragment.setInitializer(classInstanceCreation);

    VariableDeclarationStatement variableDeclarationStatement = ast.newVariableDeclarationStatement(variableDeclarationFragment);
    variableDeclarationStatement.setType(ast.newSimpleType(ast.newName(getUdtType(gspCommonField.getLabelID()))));

    block.statements().add(variableDeclarationStatement);
  }

//	private AssociationInfo getAssociationInfo(String propertyName)
//	{

//		for (var item : associationInfos)
//		{
//			if (propertyName.equals(item.SourceColumn))
//			{
//				return item;
//			}
//		}
//		throw new RuntimeException();
//	}

//
//	private void addMappingReaderStatement(java.util.ArrayList<StatementSyntax> statements)
//	{
//		statements.add(SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(TypeUtils.getTypeSyntax(CefMappingReader.class)).WithVariables(SyntaxFactory.<VariableDeclaratorSyntax>SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(varMappingReader)).WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.ObjectCreationExpression(TypeUtils.GetTypeSyntax(CefMappingReader.class)).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SeparatedList(new SyntaxNodeOrToken[]{ SyntaxFactory.Argument(SyntaxFactory.IdentifierName(getMappingFieldName())), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument(SyntaxFactory.IdentifierName(ParamReader))})))))))));
//	}
//	private void addRepoStatement(java.util.ArrayList<StatementSyntax> statements)
//	{
//		java.lang.Class dataTypeAttrType = dacContext.getReposContext().getCefContext().DataTypeAttrType;
//		Object tempVar = propertyInfo.getCustomAttribute(dataTypeAttrType);
//		NestedConfigAtrribute dataTypeAttribute = (NestedConfigAtrribute)((tempVar instanceof NestedConfigAtrribute) ? tempVar : null);
//		java.lang.Class configType = dataTypeAttribute.getConfigType();
//		Object tempVar2 = configType.Assembly.CreateInstance(configType.FullName);
//		Spi.Entity.INestedConfig nestedConfig = (Spi.Entity.INestedConfig)((tempVar2 instanceof Spi.Entity.INestedConfig) ? tempVar2 : null);

//		var configId = nestedConfig.getConfigId();
//		java.lang.Class reposFactoryType = dacContext.getReposContext().getCefContext().nestedRepositoryFactory();
//		statements.add(CreateInsGenerator.getCreateStatement(varRepoName, reposFactoryType, configId, "CreateRepository"));
//	}
//	private void addReturnStatement(java.util.ArrayList<StatementSyntax> statements)
//	{

//		var assoUdtAttributes = propertyInfo.<EnrichedAssoUdtAttribute>getCustomAttributes();
//		if (!assoUdtAttributes.Any())
//		{
//			AddNotAssUdtReturnStatement(statements);
//		}
//		else
//		{
//			AddAssUdtReturnStatement(statements, assoUdtAttributes);
//		}
//	}
//
//	private void addNotAssUdtReturnStatement(java.util.ArrayList<StatementSyntax> statements)
//	{
//		statements.add(SyntaxFactory.ReturnStatement(SyntaxFactory.BinaryExpression(SyntaxKind.AsExpression, SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("u1Repos"), SyntaxFactory.IdentifierName("ReadData"))).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SingletonSeparatedList(SyntaxFactory.Argument(SyntaxFactory.IdentifierName(varMappingReader))))), TypeUtils.getTypeSyntax(propertyInfo.PropertyType))));
//	}
//
//	private void addAssUdtReturnStatement(java.util.ArrayList<StatementSyntax> statements, Iterable<EnrichedAssoUdtAttribute> assoUdtAttributes)
//	{
//		Object tempVar = assoUdtAttributes.First();
//		EnrichedAssoUdtAttribute attr = (EnrichedAssoUdtAttribute)((tempVar instanceof EnrichedAssoUdtAttribute) ? tempVar : null);
//		LocalDeclarationStatementSyntax readDataStatement = CreateInsGenerator.getReadStatement(attr.getAssoUdtDataType(), varRepoName, varAssUdtName, varMappingReader);
//		statements.add(readDataStatement);
//		//TODO
//		CefPropertyInfo  assoProperty = getAssPropertyInfo(propertyInfo.PropertyType);
//		var assoType = assoProperty.PropertyType;
//		AddNewAssDataStatement(statements);
//		AssociationInfo assInfo = getAssociationInfo(propertyInfo.getPropertyName());
//		EntityCreateAssocationDefMethodGenerator.buildMappingStatement(statements, assInfo, EntityCreateAssocationDefMethodGenerator.varMapping);
//		EntityCreateAssocationDefMethodGenerator.buildReaderStatement(statements, propertyInfo.getPropertyName(), EntityCreateAssocationDefMethodGenerator.getMappingReader(propertyInfo.getPropertyName()), EntityCreateAssocationDefMethodGenerator.varMapping);
//		EntityCreateAssocationDefMethodGenerator.buildAdapterStatement(statements, assInfo, propertyInfo.getPropertyName(), "adapter");
//
//		//② Association Element

//		for (var refProp : assoType.getProperties())
//		{
//			if (refProp.DeclaringType != assoType)
//			{
//				continue;
//			}
//			BuildPropAssStatement(statements, assInfo, refProp, assoProperty.getName());
//			//var udtAssoPropName = CreateInsGenerator.getEntityProerty(assoProperty.Name, varAss);
//			//CreateInsGenerator.BuildAssignmnet(refProp, propertyInfo.Name, statements, udtAssoPropName);
//		}
//		statements.add(SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(varAss)));
//	}
//
//	// assValue.AssUdt1.Code3_Name=adapter.readproperty("decimal1", mappingReaderCode3);
//	private void buildPropAssStatement(java.util.ArrayList<StatementSyntax> statements, AssociationInfo associationInfo, System.Reflection.CefPropertyInfo  refProp, String propertyName)
//	{
//		statements.add(SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(varAss), SyntaxFactory.IdentifierName(propertyName)), SyntaxFactory.IdentifierName(refProp.getName())), SyntaxFactory.CastExpression(TypeUtils.getTypeSyntax(refProp.PropertyType), SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("adapter"), SyntaxFactory.IdentifierName("readproperty"))).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SeparatedList(new SyntaxNodeOrToken[]{ SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(EntityCreateAssocationDefMethodGenerator.getRefColumn(refProp.getName(), associationInfo)))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument(SyntaxFactory.IdentifierName(EntityCreateAssocationDefMethodGenerator.getMappingReader(propertyInfo.getPropertyName())))})))))));
//	}
//
//
//
	public void buildPropAssStatement(AST ast, java.util.ArrayList<Statement> statements,
                                      GspAssociation associationInfo, IGspCommonField  gspCommonField, String propertyName,GspAssociation gspAssociation, CompileUnitGeneratorContext unitCtx)
	{
    MethodInvocation getPropertyInvocation = ast.newMethodInvocation();
    getPropertyInvocation.setName(ast.newSimpleName("get"+propertyName));
    getPropertyInvocation.setExpression(ast.newSimpleName(varAss));

    MethodInvocation setRefPropInvocation = ast.newMethodInvocation();
    setRefPropInvocation.setExpression(getPropertyInvocation);
    setRefPropInvocation.setName(ast.newSimpleName("set" + gspCommonField.getLabelID()));

    MethodInvocation readPropInvocation = ast.newMethodInvocation();
    readPropInvocation.setName(ast.newSimpleName("readProperty"));
    readPropInvocation.setExpression(ast.newSimpleName("adapter"));
    readPropInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast, EntityCreateAssocationDefMethodGenerator.getRefColumn(this.dacContext.getReposContext().getCefContext().getCefGenContext(), gspCommonField.getLabelID(), associationInfo, gspCommonField, gspCommonDataType)));
    readPropInvocation.arguments().add(ast.newSimpleName(EntityCreateAssocationDefMethodGenerator.getMappingReader(propertyName)));

    CastExpression castExpression = ast.newCastExpression();
    TypeInfo info = Utils.getRefElementPropertyName(gspCommonField, gspAssociation,dacContext.getReposContext().getCefContext().getCefGenContext());
//    if(gspCommonField.getObjectType() == GspElementObjectType.Enum){
//        info = Utils.getRefEnumElementType(gspCommonField, gspAssociation,dacContext.getReposContext().getCefContext().getCefGenContext());
//    }
//    else {
//        info=  dacContext.getReposContext().getCefContext().getEntityGeneratorContext().getFieldTypeInfo(nodeCode,gspCommonField);
//    }

    info.setUseFullName(true);
    castExpression.setType(info.getType(ast, unitCtx));
    castExpression.setExpression(readPropInvocation);

    setRefPropInvocation.arguments().add(castExpression);

    statements.add(ast.newExpressionStatement(setRefPropInvocation));
  }
//
//	private void addNewAssDataStatement(java.util.ArrayList<StatementSyntax> statements)
//	{
//		statements.add(SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(getUdtType(propertyInfo.getPropertyName()))).WithVariables(SyntaxFactory.<VariableDeclaratorSyntax>SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(varAss)).WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(GetUdtType(propertyInfo.getPropertyName()))).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SingletonSeparatedList(SyntaxFactory.Argument(SyntaxFactory.IdentifierName(varAssUdtName)))))))))));
//	}

}
